import axios from "axios";
import { ElMessage } from 'element-plus'
const http = axios.create({
    baseURL: "https://www.liulongbin.top:8888/api/private/v1",
    timeout: 10000
})

// 请求拦截
http.interceptors.request.use(config => {
    return config
}, err => {
    throw new Error(err)
})


// 响应拦截
http.interceptors.response.use(res => {
    let { status, msg } = res.data.meta
    switch (status) {
        case 200:
            ElMessage({
                message: msg,
                type: 'success',
            })
            break;
        case 201:
            ElMessage({
                message: '创建成功',
                type: 'success',
            })
            break;
        case 204:
            ElMessage({
                message: '删除成功',
                type: 'success',
            })
            break;
        case 400:
            ElMessage({
                message: '请求的地址不存在或者包含不支持的参数',
                type: 'success',
            })
            break;
        case 401:
            ElMessage({
                message: '未授权',
                type: 'success',
            })
            break;
        case 403:
            ElMessage({
                message: '被禁止访问',
                type: 'success',
            })
            break;
        case 404:
            ElMessage({
                message: '请求的资源不存在',
                type: 'success',
            })
            break;
        case 422:
            ElMessage({
                message: '发生一个验证错误',
                type: 'success',
            })
            break;
        case 500:
            ElMessage({
                message: '内部错误',
                type: 'success',
            })
            break;
    }
    return res.data.data
}, err => {
    throw new Error(err)
})

export default http;